package com.tucc.hello.leetcode.no1_400.no201_400.no278;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution extends VersionControl {

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.bad = 2;
        System.out.println(solution.firstBadVersion(3));
        solution.bad = 2;
        System.out.println(solution.firstBadVersion(12321));
    }

    public int firstBadVersion(int n) {
        long left = 1;
        long right = Integer.MAX_VALUE;
        if (isBadVersion(n)) {
            right = n;
        } else {
            left = n + 1;
        }
        while (left < right) {
            long mid = (left + right) / 2;
            if (isBadVersion((int) mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        if (isBadVersion((int) left)) {
            return (int) left;
        }
        return (int) right;
    }
}

class VersionControl {

    public int bad;

    public boolean isBadVersion(int n) {
        return n >= bad;
    }
}
